473,461 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Validating TextField dependent on List/Menu value

Hallo NG,

I am new to JavaScript and would really appreciate any help to solve my
problem.
I am using the blow code in my form to validate form fields. What I
would like to accomplish is that if when the list/menu (attribute6)
value is "Ja" then to make the TextField Pas Nr (attribute4)
required. And if when the list/menu (attribute6) value is "Nee" to
make the TextField Pas Nr (attribute4) not required.

I have tried to adjust the code with no success. I give up and hope
that someone can help me to the right direction!

Thank you in advance,
-Platostoteles

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="41%" border="0" align="center" id="0">
<tr>
<td height="291" colspan="2" align="left" valign="top">
<!-- newsletter -->
<script language="JavaScript" type="text/javascript">
var fieldstocheck = new Array();
fieldnames = new Array();

function checkform() {
for (i=0;i<fieldstocheck.length;i++) {
if
(eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value")
== "") {
alert("Vul a.u.b. het volgende verplichte veld in:
"+fieldnames[i]);

eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
return false;
}
}

if(! compareEmail())
{
alert("De ingevulde Email adressen komen niet overeen");
return false;
}
return true;
}
function addFieldToCheck(value,name) {
fieldstocheck[fieldstocheck.length] = value;
fieldnames[fieldnames.length] = name;
}

function compareEmail()
{
return (document.subscribeform.elements["email"].value ==
document.subscribeform.elements["emailconfirm"].value);
}
</script> <form
action="http://www.whatever.com/newsletter/lists/?p=subscribe"
name="subscribeform" method="post" target="_self">
<div align="left"><font size="2" face="Verdana">
<input type="checkbox" name="list[1]" value=signup checked />
Nieuwsbrief 'Impuls' </font></div>
<div align="left" class="listdescription"> <font size="2"
face="Verdana">
<input type=hidden name="listname[1]" value="Nieuwsbrief
Impuls"/>
</font></div>
<div align="left"><font size="2" face="Verdana">
<input name="list[2]" type="checkbox" value=signup checked
/>
Aanbiedingen voor niet-leden</font></div>
<div align="left" class="listdescription">
<input type=hidden name="listname[2]" value="Aanbiedingen
voor niet-leden"/>
<br>
</div>
<table border=0>
<tr>
<td width="96"><div class="required">Email:</div></td>
<td width="260" class="attributeinput"><input type=text
name=email value="" size="40">
<script language="JavaScript" type="text/javascript">
addFieldToCheck("email","Email");
</script></td>
</tr>
<tr>
<td>Bevestig email:</td>
<td class="attributeinput"><input type=text
name=emailconfirm size="40">
<script language="JavaScript" type="text/javascript">
addFieldToCheck("emailconfirm","Bevestig email");
</script></td>
</tr>
<tr>
<td><font size="2" face="Verdana">Lid:</font></td>
<td class="attributeinput"><font size="2" face="Verdana">
<select name="attribute6" id="select2">
<option value="3">Ja</option>
<option value="4">Nee</option>
</select>
<!--0-->
<script language="JavaScript" type="text/javascript">
addFieldToCheck("attribute6","Lid");
</script>
</font></td>
</tr>
<tr>
<td><div class="required"><font size="2" face="Verdana">Pas
nr.</font></div></td>
<td class="attributeinput"><font size="2" face="Verdana">
<input name="attribute4" type="text" id="PassNr2"
size="15" maxlength="15">
<script language="JavaScript" type="text/javascript">
addFieldToCheck("attribute4","Pas nr");
</script>
</font> </td>
</tr>
<input type=hidden name="htmlemail" value="1">
</table>
<div align="left"><font size="2" face="Verdana"><br>
<input name="subscribe" type="submit" id="subscribe"
value="Aanmelden" onClick="return checkform();">
<br>
<br>
<br>
</font> </div>
</form>
<font size="2" face="Verdana">&nbsp; </font></td>
</tr>
</table>
</body>
</html>

Jan 19 '06 #1
1 5894
pl***********@gmail.com wrote:
Hallo NG,

I am new to JavaScript and would really appreciate any help to solve my
problem.
I am using the blow code in my form to validate form fields. What I
would like to accomplish is that if when the list/menu (attribute6)
value is "Ja" then to make the TextField Pas Nr (attribute4)
required. And if when the list/menu (attribute6) value is "Nee" to
make the TextField Pas Nr (attribute4) not required.

I have tried to adjust the code with no success. I give up and hope
that someone can help me to the right direction!

Thank you in advance,
-Platostoteles

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
It might be time to change to strict, given that it has been around for
6 years now.
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="41%" border="0" align="center" id="0">
When posting code, keep examples to a minimum that show the problem.
Otherwise you are posting lots of stuff that just isn't necessary.

<tr>
<td height="291" colspan="2" align="left" valign="top">
<!-- newsletter -->
<script language="JavaScript" type="text/javascript">
The language attribute is deprecated, keep type.

var fieldstocheck = new Array();
fieldnames = new Array();
I'll guess that fieldstocheck will contain the names of form elements to
check? But you haven't overcome the issue of how to put the test into
the array - there is no simple answer to that.


function checkform() {
Instead of using a submit button onclick attribute to call checkform(),
use the form onsubmit attribute. If you pass a reference to the form
from the onclick (see below), you don't have to worry about getting a
reference to the form from the function:

function checkform(form) {

for (i=0;i<fieldstocheck.length;i++) {
Variables should be kept local wherever possible, especially counters
like 'i'.

But anyhow, all you want to do is:

var t = form.attribute4[form.attribute4.selectedIndex].value;
if ('Ja' == t && "" != form.attribute6.value){
alert("Vul a.u.b. het volgende verplichte veld in Pas nr.";
if (form.attribute6.focus) form.attribute6.focus();
return false;
}
if
(eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value")
== "") {
eval is almost never required and is not needed here at all. The
equivalent would be:

if (document.subscribeform.elements[fieldstocheck[i]].value == "") {

but I've changed that line anyway.
[...]
</script> <form
Add an onsubmit handler and pass a reference to the form using 'this':

onsubmit="return checkform(this);"
If checkform returns false, the form will not submit.

action="http://www.whatever.com/newsletter/lists/?p=subscribe"
name="subscribeform" method="post" target="_self">
<div align="left"><font size="2" face="Verdana">
<input type="checkbox" name="list[1]" value=signup checked />
This is HTML, so use HTML markup not faux XHTML - ditch the '/':

<input type="checkbox" name="list[1]" value="signup" checked>
You are also better off to always enclose attribute values in quotes,
even when not required.

Nieuwsbrief 'Impuls' </font></div>
<div align="left" class="listdescription"> <font size="2"
face="Verdana">


The font element is deprecated in HTML 4, use CSS instead. There is no
need to put all those divs in there, just use one div, style it
appropriately and use BR to break lines. Forms must contain a block
element, so wrap all the elements inside the form in a single div.

The above is untested, but should give you an idea of what to do.

[...]

--
Rob
Jan 20 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mats | last post by:
It's good practice to validate input, not only where it should be coming from, but from anywhere it's possible to change or add input for a "client". If all user input is transfered using "post"...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
1
by: paakwesi | last post by:
I'm looking to modify the javascript behavior on http://research.yale.edu/%7Ekamusi/exercises/learners/index.php to do two things: Initial State: Both menus are populated with all their entries...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
16
by: shyamg | last post by:
Hi, this is my javascript validating the fields in mozilla FF but its working and validating only one field. how to write the and how to works the script .................. function...
26
by: jmartmem | last post by:
Greetings, I have an ASP page with two dynamic dependent list boxes written in JavaScript. My dependent lists work great, but my problem is that the values for "Program_Name" and "Project_Name"...
2
by: Hugh | last post by:
The PyGUI website specified this place as the place for general discussion about it, so here goes.... First off - thanks for something that is so straightforward to get running on OSX... I've...
1
by: jmartmem | last post by:
Greetings, I have a nagging problem with client-side dynamic dependent list boxes that perhaps someone can help me troubleshoot. I have a form with a series of dynamic dependent list boxes....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.